package com.micronaut.cloud.common;

import java.io.Serial;
import java.io.Serializable;

import com.micronaut.cloud.enums.ResultCodeEnum;

import io.micronaut.serde.annotation.SerdeImport;
import lombok.Data;

/***
 * @description: 返回结果处理
 * @author: zhuhb
 * @date: 2022/4/12 13:58
 */
@Data
@SerdeImport
public class ResultVO<T> implements Serializable {


    @Serial
    private static final long serialVersionUID = 1L;


    private int code;

    private String message;

    private T result;


    public boolean success() {
        return this.code == ResultCodeEnum.SUCCESS.code();
    }


    public boolean fail() {
        return this.code != ResultCodeEnum.SUCCESS.code();
    }


    public static <T> ResultVO<T> common(ResultCodeEnum resultCode, T result, String message) {
        return new ResultVO<T>(resultCode.code(), message, result);
    }

    public static <T> ResultVO<T> common(int code, T result, String message) {
        return new ResultVO<T>(code, message, result);
    }


    public static <T> ResultVO<T> success(T result) {
        return new ResultVO<T>(ResultCodeEnum.SUCCESS.code(), ResultCodeEnum.SUCCESS.desc(), result);
    }

    public static <T> ResultVO<T> success(String message) {
        return new ResultVO<T>(ResultCodeEnum.SUCCESS.code(), message);
    }

    public static <T> ResultVO<T> success(T result, String message) {
        return new ResultVO<T>(ResultCodeEnum.SUCCESS.code(), message, result);
    }

    public static <T> ResultVO<T> success(ResultCodeEnum resultCode, T result, String message) {
        return new ResultVO<T>(resultCode.code(), message, result);
    }

    public static <T> ResultVO<T> success(int code, String message) {
        return new ResultVO<T>(code, message);
    }

    public static <T> ResultVO<T> success(int code, String message, T result) {
        return new ResultVO<T>(code, message, result);
    }

    public static <T> ResultVO<T> error(String message) {
        return new ResultVO<T>(ResultCodeEnum.ERROR.code(), message);
    }

    public static <T> ResultVO<T> error(ResultCodeEnum resultCode, String message) {
        return new ResultVO<T>(resultCode.code(), message);
    }

    public static <T> ResultVO<T> error(int code, String message) {
        return new ResultVO<T>(code, message);
    }

    public static <T> ResultVO<T> error(ResultCodeEnum resultCodeEnum) {
        return new ResultVO<>(resultCodeEnum.code(), resultCodeEnum.desc());
    }

    public static <T> ResultVO<T> error(int code, String message, T result) {
        return new ResultVO<T>(code, message, result);
    }
    public static <T> ResultVO<T> error(T result, String message) {
        return new ResultVO<T>(ResultCodeEnum.ERROR.code(), message, result);
    }
    public static <T> ResultVO<T> paramError(String message) {
        return new ResultVO<T>(ResultCodeEnum.PARAMETER_ERROR.code(), message);
    }

    public static <T> ResultVO<T> paramError(ResultCodeEnum resultCode, String message) {
        return new ResultVO<T>(resultCode.code(), message);
    }

    public ResultVO() {
        super();
    }

    public ResultVO(int code, String message) {
        super();
        this.code = code;
        this.message = message;
    }

    public ResultVO(int code, String message, T result) {
        super();
        this.code = code;
        this.message = message;
        this.result = result;
    }
}
